package com.ruoyi.course.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.CoursesMapper;
import com.ruoyi.course.domain.Courses;
import com.ruoyi.course.service.ICoursesService;

import javax.annotation.Resource;


/**
 * 课程详情管理Service业务层处理
 * 
 * @author gzq
 * @date 2025-08-12
 */
@Service
public class CoursesServiceImpl implements ICoursesService 
{
    @Autowired
    private CoursesMapper coursesMapper;
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    private static final String COURSE_ZSET_KEY = "course:top:zset";
    /**
     * 查询课程详情管理
     * 
     * @param id 课程详情管理主键
     * @return 课程详情管理
     */

    @Override
    public Courses selectCoursesById(Integer id)
    {
        return coursesMapper.selectCoursesById(id);
    }

    /**
     * 查询课程详情管理列表
     * 
     * @param courses 课程详情管理
     * @return 课程详情管理
     */
    @Override
    public List<Courses> selectCoursesList(Courses courses)
    {
        return coursesMapper.selectCoursesList(courses);
    }

    /**
     * 新增课程详情管理
     * 
     * @param courses 课程详情管理
     * @return 结果
     */
    @Override
    public int insertCourses(Courses courses)
    {
        courses.setCreateTime(DateUtils.getNowDate());
        return coursesMapper.insertCourses(courses);
    }

    /**
     * 修改课程详情管理
     * 
     * @param courses 课程详情管理
     * @return 结果
     */
    @Override
    public int updateCourses(Courses courses)
    {
        return coursesMapper.updateCourses(courses);
    }

    /**
     * 批量删除课程详情管理
     * 
     * @param ids 需要删除的课程详情管理主键
     * @return 结果
     */
    @Override
    public int deleteCoursesByIds(Integer[] ids)
    {
        return coursesMapper.deleteCoursesByIds(ids);
    }

    /**
     * 删除课程详情管理信息
     * 
     * @param id 课程详情管理主键
     * @return 结果
     */
    @Override
    public int deleteCoursesById(Integer id)
    {
        return coursesMapper.deleteCoursesById(id);
    }

    @Override
    public List<Courses> selectVideoDetail(Courses courses) {


        System.out.println(coursesMapper.selectVideoDetail(courses).size());

        return coursesMapper.selectVideoDetail(courses);
    }

        @Override
        public List<Courses> selectHost(Courses courses) {

            List<Courses> courseList = coursesMapper.selectVideoDetail(courses);

            System.out.println("课程列表数据"+courseList.size());

            ZSetOperations<String, Object> zSetOps = redisTemplate.opsForZSet();
            Long zsetSize = zSetOps.size(COURSE_ZSET_KEY);

            if (zsetSize == null || zsetSize == 0) {

                if (courseList == null || courseList.isEmpty()) {
                    return new ArrayList<>();
                }

                redisTemplate.delete(COURSE_ZSET_KEY);
                for (Courses course : courseList) {
                    System.out.println("课程"+course);

                    int score = course.getParticipantCount() != null
                            ? Integer.parseInt(course.getParticipantCount().toString()) : 0;
                    zSetOps.add(COURSE_ZSET_KEY, course, score);
                }



            }
            Set<ZSetOperations.TypedTuple<Object>> tuples = zSetOps.reverseRangeWithScores(COURSE_ZSET_KEY, 0, 9);
            List<Courses> top10List = new ArrayList<>();
            if (tuples != null) {
                for (ZSetOperations.TypedTuple<Object> tuple : tuples) {
                    Object value = tuple.getValue();
                    if (value instanceof Courses) {
                        top10List.add((Courses) value);
                    }
                }
            }

            return top10List;
        }

    @Override
    public Courses selectDetailById(Integer id) {
        return coursesMapper.selectDetailById(id);
    }
}
